package com.example.coding_problem.Fragment;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.coding_problem.R;
import com.example.coding_problem.SQLenvironmental_indicators;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

import static android.support.constraint.Constraints.TAG;

public class Environmental_indicators extends Fragment {
    private TextView tv_temperature1;
    private FrameLayout RL_temperature;
    private TextView tv_humidity1;
    private SQLiteDatabase db;
    private TextView tv_Illumination1;
    private TextView tv_CQ21;
    private ListView lv;
    private int j=0;
    private int i=0;
    private TextView tv_PM1;
    private TextView tv_road_condition1;
    MyAdapter myAdapter=new MyAdapter();
    private List<Environmental_indicators_List>ei_list=new ArrayList<>();
    public Environmental_indicators() {
    }
    public interface BlogService{
        @GET("aircondition")
        Call<ResponseBody> getBlog();
    }
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable(){
        @Override
        public void run() {
            initData();
            handler.postDelayed(this,3000);
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.environmental_indicators_list, container, false);
        initView(view);
        initData();
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable,3000);
        return view;
    }
    private void initView(View view) {
        lv=view.findViewById(R.id.lvw);
    }
    private void initData(){
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://hh1.me:5001/")
                    .build();
            BlogService service=retrofit.create(BlogService.class);
            Call<ResponseBody> call=service.getBlog();
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    Gson gson=new Gson();
                    Environmental_indicators_List carlist= null;
                    try {
                        carlist = gson.fromJson(response.body().string(), Environmental_indicators_List.class);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ei_list.clear();
                    lv.setAdapter(new MyAdapter());
                    ei_list.add(carlist);
                }
                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return ei_list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(getContext(),R.layout.environmental_indicators,null);
            TextView tv_temperature=view1.findViewById(R.id.tv_temperature1);
            TextView tv_humidity=view1.findViewById(R.id.tv_humidity1);
            TextView tv_CQ2=view1.findViewById(R.id.tv_CQ21);
            RelativeLayout RL_temperature=view1.findViewById(R.id.RL_temperature);
            RelativeLayout RL_humidity=view1.findViewById(R.id.RL_humidity);
            RelativeLayout RL_Illumination=view1.findViewById(R.id.RL_Illumination);
            RelativeLayout RL_CQ2=view1.findViewById(R.id.RL_CQ2);
            RelativeLayout PL_PM=view1.findViewById(R.id.PL_PM);
            RelativeLayout RL_road_condition=view1.findViewById(R.id.RL_road_condition);
            TextView tv_PM=view1.findViewById(R.id.tv_PM1);
            TextView tv_road_condition1=view1.findViewById(R.id.tv_road_condition1);
            TextView tv_Illumination1=view1.findViewById(R.id.tv_Illumination1);
            tv_temperature.setText(ei_list.get(0).getWendu()+"");
            tv_humidity.setText(ei_list.get(0).getShidu()+"");
            tv_CQ2.setText(ei_list.get(0).getCo2()+"");
            Log.i(TAG, "123456798: "+ei_list.get(0).getCo2());
            tv_PM.setText(ei_list.get(0).get_$Pm25288()+"");
            tv_road_condition1.setText(ei_list.get(0).get拥挤程度().get(i)+"");
            //随机数
            int min = 10;
            int max = 99;
            Random random = new Random();
            int num = random.nextInt(max)%(max-min+1) + min;
            int j=0;
            j=num;
            tv_Illumination1.setText(num+"");
            Log.i(TAG, "getView: "+tv_Illumination1);
            if (ei_list.get(0).getWendu() <= 20){
                RL_temperature.setBackgroundColor(Color.parseColor("#ed1941"));
            }  if (ei_list.get(0).getShidu()<=50) {
                RL_humidity.setBackgroundColor(Color.parseColor("#ed1941"));
            }  if (num<=50) {
                RL_Illumination.setBackgroundColor(Color.parseColor("#ed1941"));
            }  if (ei_list.get(0).getCo2()<=50) {
                RL_CQ2.setBackgroundColor(Color.parseColor("#ed1941"));
            } if (ei_list.get(0).get_$Pm25288()<=300) {
                PL_PM.setBackgroundColor(Color.parseColor("#ed1941"));
            }  if (ei_list.get(0).get拥挤程度().get(i)<=1) {
                RL_humidity.setBackgroundColor(Color.parseColor("#ed1941"));
            }
            //创建一个帮助类对象
            SQLenvironmental_indicators mySqliteOpenHelper = new SQLenvironmental_indicators(getContext());
            //调用getReadableDatabase方法,来初始化数据库的创建
            db= mySqliteOpenHelper.getReadableDatabase();
//db.execSQL("create table info(_id integer PRIMARY KEY AUTOINCREMENT,temperature varchar(20),humidity varchar(20),Illumination varchar(20),CQ2 varchar(20),pm varchar(20),road_condition varchar(20))");
            //把数据库存放到数据库
            ContentValues values=new ContentValues();
            values.put("_id",i);
            values.put("temperature",ei_list.get(0).getWendu());
            values.put("humidity",ei_list.get(0).getShidu());
            values.put("Illumination",j);
            values.put("CQ2",ei_list.get(0).getCo2());
            values.put("pm",ei_list.get(0).get_$Pm25288());
            values.put("road_condition",ei_list.get(0).get拥挤程度().get(i));
            db.insert("info",null,values);
            Cursor cursor=db.query("info",null,null,null,
                    null,null,null,null);
            cursor.moveToLast();
                cursor.close();
            return view1;
        }
    }
}

